package com.baofu.paycore.biz.paycore.handler;

import com.baofu.paycore.biz.external.clear.ClearQueryService;
import com.baofu.paycore.biz.external.clear.model.ClearFundResBO;
import com.baofu.paycore.biz.paycore.handler.impl.RechargeHandler;
import com.baofu.paycore.biz.paycore.handler.impl.WithdrawHandler;
import com.baofu.paycore.common.exception.PayCoreBizException;
import com.baofu.paycore.dal.model.ClearBankDO;
import com.baofu.paycore.manager.model.RechargeBO;
import com.baofu.paycore.manager.model.withdraw.WithdrawBO;
import com.baofu.paycore.manager.storage.impl.ClearBankManager;
import com.baofu.paycore.service.facade.enums.PayCoreErrorCode;
import com.baofu.paycore.service.facade.enums.PayStatusEnums;
import com.baofu.paycore.service.facade.enums.ServiceCodeEnum;
import com.system.commons.exception.BizServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * 支付订单状态重发处理
 *
 * User: xin deng  Date: 2017/6/13 ProjectName: paycore Version: 1.0
 */
@Slf4j
@Component
public class PayStatusRetryHandler {

    /**
     * 渠道银行manager
     */
    @Autowired
    private ClearBankManager clearBankManager;

    /**
     * 提现handler类
     */
    @Autowired
    private WithdrawHandler withdrawHandler;

    /**
     * 充值处理类
     */
    @Autowired
    private RechargeHandler rechargeHandler;

    /**
     * 清分查询服务
     */
    @Autowired
    private ClearQueryService clearQueryService;

    /**
     * 账务重发业务处理
     *
     * @param payNo     支付单号
     */
    public String retryAccountHandler(String payNo, String serviceCode) {

        //先查询支付渠道单状态
        ClearBankDO clearBankDO = clearBankManager.queryClearBankInfo(payNo);
        if (clearBankDO == null) {
            log.error("账务重发业务处理,支付单号:{}", payNo);
            throw new BizServiceException(PayCoreErrorCode.ORDER_IS_NOT_EXISTED, "清分渠道信息不存在");
        }
        if (!PayStatusEnums.SUCCESS.getCode().equals(clearBankDO.getStatus())) {
            log.error("清分渠道未成功:{},账务不进行重发处理", clearBankDO.getStatus());
            throw new BizServiceException(PayCoreErrorCode.CLEAR_STATUS_NOT_SUCCESS, "清分单状态未成功");
        }
        //查询清分渠道单信息
        ClearFundResBO clearFundResBO;
        ServiceCodeEnum serviceCodeEnum = ServiceCodeEnum.explain(serviceCode);
        switch (serviceCodeEnum) {
            case WITHDRAW:
                clearFundResBO = clearQueryService.queryWithdrawByRequestOrderNo(payNo, clearBankDO.getFundDetailNo());
                break;
            default:
                clearFundResBO = clearQueryService.queryByRequestOrderNo(payNo, clearBankDO.getFundDetailNo());
        }
        if (!PayStatusEnums.SUCCESS.getCode().equals(clearFundResBO.getPayStat())) {
            log.error("账务重发业务处理,查询清分订单状态:{},不进行账务重发", clearFundResBO.getPayStat());
            throw new BizServiceException(PayCoreErrorCode.CLEAR_STATUS_NOT_SUCCESS);
        }
        return processRetry(payNo,serviceCode);

    }

    private String processRetry(String payNo, String serviceCode) {
        ServiceCodeEnum serviceCodeEnum = ServiceCodeEnum.explain(serviceCode);
        String result;
        switch (serviceCodeEnum) {
            case WITHDRAW:
                WithdrawBO withdrawBO = withdrawHandler.queryOrderByPayNo(payNo);
                withdrawHandler.checkRetryStatus(withdrawBO);
                result = withdrawHandler.processRetry(withdrawBO);
                break;
            case RECHARGE:
                RechargeBO rechargeBO = rechargeHandler.queryOrderByPayNo(payNo);
                result = rechargeHandler.processRetry(rechargeBO);
                break;
            default:
                log.error("补单 暂不支持此业务:{}", serviceCode);
                throw new PayCoreBizException(PayCoreErrorCode.NOT_IN_SERVICE);
        }
        return result;
    }
}
